<?php

/**
 * QQ新浪授权登录
 */
class MembersOauthModel extends BaseModel {

    public function tableName() {
        return '{{members_oauth}}';
    }

    public function rules() {
        return array(
            array('uid', 'required', 'message' => '获取用户失败！'),
            array('token', 'required', 'message' => 'token不能为空！'),
            array('openid', 'required', 'message' => 'openid不能为空！'),
            array('regdate,bind_type', 'safe'),
        );
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 是否绑定本地帐号
     * @param type $type 类型
     * @param type $bindInfo 绑定信息
     * @return boolean
     */
    public function isBind($type, $bindInfo) {
        if (empty($type) || empty($bindInfo)) {
            return false;
        }
        $info = $this->find($this->where(array('bind_type' => $type, 'openid' => $bindInfo['openid'])));
        if (empty($info)) {
            return false;
        }
        $info->token = $bindInfo['token'];
        $info->regdate = $bindInfo['regdate'];
        $info->save();
        return $info->attributes;
    }

    /**
     * 增加绑定关系
     * @param type $type 类型
     * @param type $bindInfo 绑定信息
     * @param type $uid 需要绑定的用户UID
     * @return boolean
     */
    public function addBind($type, $bindInfo, $uid) {
        if (empty($type) || empty($bindInfo) || empty($uid)) {
            $this->addError('', '绑定信息不完整无法绑定！');
            return false;
        }
        $this->uid = $uid;
        $this->token = $bindInfo['token'];
        $this->openid = $bindInfo['openid'];
        $this->regdate = $bindInfo['regdate'];
        $this->bind_type = $type;
        $this->setIsNewRecord(true);
        if ($this->validate() && $this->save()) {
            return true;
        }
        return false;
    }

    /**
     * 检查用户是否已经绑定过
     * @param type $type 类型
     * @param type $uid 用户UID
     * @return boolean true 已经绑定过，false没有绑定
     */
    public function isUserBind($type, $uid) {
        if (empty($type) || empty($uid)) {
            return false;
        }
        $count = $this->count($this->where(array('bind_type' => $type, 'uid' => $uid)));
        return $count ? true : false;
    }

    /**
     * 删除会员的全部绑定记录
     * @param type $uid 用户UID
     * @param type $type 类型
     * @return boolean
     */
    public function deleteBind($uid, $type = '') {
        if (empty($uid)) {
            return false;
        }
        $where = array(
            'uid' => $uid,
        );
        if ($type) {
            $where['bind_type'] = $type;
        }
        return $this->deleteAll($this->where($where));
    }

    /**
     * 自动绑定当前登录帐号
     * @param type $session_array 
     * @return boolean
     */
    public function autoBind($session_array) {
        if (Yii::app()->user->isLogged()) {
            if ($this->addBind($session_array['bind_type'], $session_array, Yii::app()->user->uid)) {
                return true;
            } 
        }
        return false;
    }

    /**
     * 返回用户绑定列表
     * @param type $uid 用户UID
     * @return string
     */
    public function getBundList($uid) {
        $arrBundList = array('qq', 'sina');
        $arrReturn = array('qq' => '', 'sina' => '');
        $objCriteria = new CDbCriteria(array(
            'condition' => 'uid = :uid',
            'params' => array('uid' => $uid),
        ));
        $objModels = $this->findAll($objCriteria);
        foreach ($objModels as $objModel) {
            $bind_type = strtolower($objModel->bind_type);
            if (in_array($bind_type, $arrBundList)) {
                $arrReturn[$bind_type] = $objModel;
            } else {
                $arrReturn[$bind_type] = '';
            }
        }
        return $arrReturn;
    }

}
